package com.example.healkep;

import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.healkep.adapter.DoctorListAdapter;
import com.example.healkep.bean.DoctorListBean;
import com.example.healkep.utils.FastJsonUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DoctorListActivity extends AppCompatActivity {

    private RecyclerView recyclerview;
    private DoctorListAdapter mDoctorListAdapter;
    private List<DoctorListBean> doctorListBeans = new ArrayList<>();

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor_list);

        recyclerview = findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));

        getData();

    }

    private void getData() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(LoginActivity.url + "/doctorInfo/list")
                .build();
        Call call = client.newCall(request);

        //异步调用,并设置回调函数
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Toast.makeText(DoctorListActivity.this, "get failed", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                final String res = response.body().string();
                doctorListBeans = FastJsonUtils.jsonToArray(res, DoctorListBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(DoctorListActivity.this, "res", Toast.LENGTH_SHORT).show();
                        mDoctorListAdapter = new DoctorListAdapter(getApplicationContext(), doctorListBeans);
                        recyclerview.setAdapter(mDoctorListAdapter);
                    }
                });
            }
        });
    }
}